/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ReservaLista.java
 *
 * Created on 29/10/2010, 01:04:26 AM
 */

package pe.edu.upla.lamerced.desk.view.hospedaje;

import java.util.List;
import javax.swing.JDesktopPane;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import pe.edu.upla.lamerced.Inicial;
import pe.edu.upla.lamerced.bean.Habitacion;
import pe.edu.upla.lamerced.bean.Hospedaje;
import pe.edu.upla.lamerced.bean.Tipohabitacion;
import pe.edu.upla.lamerced.service.HabitacionService;
import pe.edu.upla.lamerced.service.HospedajeService;

/**
 *
 * @author elvis
 */
public class ReservaLista extends javax.swing.JInternalFrame {

   private HospedajeService hospedajeService;
   private HabitacionService habitacionService;
   private JDesktopPane escritorio;
   private List<Hospedaje> lstReservas;

    /** Creates new form ReservaLista */
    public ReservaLista() {
        initComponents();
    }

   public JDesktopPane getEscritorio() {
      return escritorio;
   }
   public void setEscritorio(JDesktopPane escritorio) {
      this.escritorio = escritorio;
   }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel2 = new javax.swing.JLabel();
        cbtipohabitacion = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        txtFecha = new javax.swing.JTextField();
        listaReservas = new javax.swing.JScrollPane();
        tbListaReservas = new javax.swing.JTable();
        btnNuevaReserva = new javax.swing.JButton();
        btnNuevoHospedaje = new javax.swing.JButton();
        btnAnulaHospedaje = new javax.swing.JButton();
        btnBuscarReservas = new javax.swing.JButton();

        setClosable(true);
        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Lista de reservas");

        jLabel2.setText("Tipo de habitacion");

        cbtipohabitacion.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cbtipohabitacion.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cbtipohabitacionItemStateChanged(evt);
            }
        });
        cbtipohabitacion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbtipohabitacionActionPerformed(evt);
            }
        });

        jLabel4.setFont(new java.awt.Font("Tahoma", 3, 14));
        jLabel4.setText("Lista de Reservas");

        jLabel6.setText("Fecha");

        tbListaReservas.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null}
            },
            new String [] {
                "Habitacion", "Cliente", "Fecha"
            }
        ));
        listaReservas.setViewportView(tbListaReservas);

        btnNuevaReserva.setText("Nueva");
        btnNuevaReserva.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNuevaReservaActionPerformed(evt);
            }
        });

        btnNuevoHospedaje.setText("hospedaje");
        btnNuevoHospedaje.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNuevoHospedajeActionPerformed(evt);
            }
        });

        btnAnulaHospedaje.setText("Anular");
        btnAnulaHospedaje.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAnulaHospedajeActionPerformed(evt);
            }
        });

        btnBuscarReservas.setText("Buscar");
        btnBuscarReservas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuscarReservasActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(43, 43, 43)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel6)
                    .addComponent(jLabel2))
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(txtFecha, javax.swing.GroupLayout.DEFAULT_SIZE, 167, Short.MAX_VALUE)
                            .addComponent(cbtipohabitacion, 0, 167, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnBuscarReservas)))
                .addGap(72, 72, 72))
            .addGroup(layout.createSequentialGroup()
                .addGap(87, 87, 87)
                .addComponent(btnNuevaReserva)
                .addGap(26, 26, 26)
                .addComponent(btnNuevoHospedaje)
                .addGap(38, 38, 38)
                .addComponent(btnAnulaHospedaje)
                .addContainerGap(107, Short.MAX_VALUE))
            .addComponent(listaReservas, javax.swing.GroupLayout.DEFAULT_SIZE, 467, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(btnBuscarReservas, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(cbtipohabitacion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtFecha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel6))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnNuevaReserva)
                    .addComponent(btnAnulaHospedaje)
                    .addComponent(btnNuevoHospedaje))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(listaReservas, javax.swing.GroupLayout.DEFAULT_SIZE, 223, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnNuevaReservaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNuevaReservaActionPerformed
       // TODO add your handling code here:
       ReservaNueva rn = new ReservaNueva();
       rn.setEscritorio(this.getEscritorio());
       this.getEscritorio().add(rn);
       rn.cargaInicial();
       rn.setVisible(true);
       this.dispose();
    }//GEN-LAST:event_btnNuevaReservaActionPerformed

    private void cbtipohabitacionItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbtipohabitacionItemStateChanged
       // TODO add your handling code here:
    }//GEN-LAST:event_cbtipohabitacionItemStateChanged

    private void cbtipohabitacionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbtipohabitacionActionPerformed
      
    }//GEN-LAST:event_cbtipohabitacionActionPerformed

    private void btnBuscarReservasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuscarReservasActionPerformed
       // TODO add your handling code here:
       //obtengo los valores del combo
       //obtengo los valores de la fecha
       //recargo la lista con estos valores
       int cb = cbtipohabitacion.getSelectedIndex();
       habitacionService.listaTipoHabitacion().get(cb);
       Habitacion hab = new Habitacion();
       hab.setTipohabitacion(habitacionService.listaTipoHabitacion().get(cb));
       Hospedaje hos = new Hospedaje();
       hos.setHabitacion(hab);
       hos.setEstado(1);
       lstReservas = hospedajeService.listarRserva(hos);
       pintagrilla();
    }//GEN-LAST:event_btnBuscarReservasActionPerformed

    private void btnAnulaHospedajeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAnulaHospedajeActionPerformed
       // TODO add your handling code here:
       if (tbListaReservas.getSelectedRow()!=-1){
          hospedajeService.anularReserva(lstReservas.get(tbListaReservas.getSelectedRow()).getIdhospedaje());
          JOptionPane.showMessageDialog(this, "Reserva anulada");
          Hospedaje hos = new Hospedaje();
          hos.setEstado(1);
          lstReservas = hospedajeService.listarRserva(hos);
          pintagrilla();
       }else{
          JOptionPane.showMessageDialog(this, "no ha seleccioando una resrva");
       }


    }//GEN-LAST:event_btnAnulaHospedajeActionPerformed

    private void btnNuevoHospedajeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNuevoHospedajeActionPerformed
      if (tbListaReservas.getSelectedRow()!=-1){
          HospedajeReservaNuevo form = new HospedajeReservaNuevo();
          form.setEscritorio(escritorio);
          escritorio.add(form);
          form.setReserva(lstReservas.get(tbListaReservas.getSelectedRow()));
          form.cargaInicial();
          form.setVisible(true);
          this.setVisible(false);
       }else{
       JOptionPane.showMessageDialog(this, "no ha seleccionado una reserva");
       }
    }//GEN-LAST:event_btnNuevoHospedajeActionPerformed

 

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAnulaHospedaje;
    private javax.swing.JButton btnBuscarReservas;
    private javax.swing.JButton btnNuevaReserva;
    private javax.swing.JButton btnNuevoHospedaje;
    private javax.swing.JComboBox cbtipohabitacion;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JScrollPane listaReservas;
    private javax.swing.JTable tbListaReservas;
    private javax.swing.JTextField txtFecha;
    // End of variables declaration//GEN-END:variables

   public void cargaInicial(){
      if(hospedajeService==null){
        hospedajeService = (HospedajeService)Inicial.getFactory().getBean("hospedajeService");
      }
      if(habitacionService==null){
         habitacionService = (HabitacionService)Inicial.getFactory().getBean("habitacionService");
      }
      cbtipohabitacion.removeAllItems();
      for(Tipohabitacion th : habitacionService.listaTipoHabitacion()){
         cbtipohabitacion.addItem(th.getNombre());
      }
      Hospedaje hos = new Hospedaje();
      hos.setEstado(1);
      lstReservas = hospedajeService.listarRserva(hos);
      pintagrilla();
   }
    public void pintagrilla(){
        DefaultTableModel tabla = (DefaultTableModel) tbListaReservas.getModel();
        tabla.setRowCount(0);
        for (Hospedaje examen : lstReservas) {
            Object[] fila = new Object[3];
            fila[0] = examen.getHabitacion().getNumero();
            fila[1] = examen.getUsersByIdUsuarioCli().getNombrecompleto();
            fila[2] = examen.getFecReserva();
            tabla.addRow(fila);
        }
    }


}
